package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjTrain;
import com.pmcc.cj.entity.CjTrainDetail;
import com.pmcc.cj.repository.CjTrainDetailRepository;
import com.pmcc.cj.repository.CjTrainRepository;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by tomas on 2020/9/12.
 */
@Transactional
@Service
public class CjTrainService {
    @Autowired
    CjTrainRepository cjTrainRepository;
    @Autowired
    CjTrainDetailRepository cjTrainDetailRepository;

    public ResultBean queryTrainByPage(AntdPage antdPage, CjTrain model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjTrainRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<CjTrain> getSpecification(CjTrain model) {
        return new Specification<CjTrain>() {
            @Override
            public Predicate toPredicate(Root<CjTrain> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtil.isNotEmpty(model.getTitleTrain())){
                    list.add(builder.like(root.get("titleTrain").as(String.class),  "%"+model.getTitleTrain().trim()+"%"));
                }
                if(model.getFettle() != null){
                    list.add(builder.equal(root.get("fettle").as(Integer.class),  model.getFettle()));
                }
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    public ResultBean addTrain(CjTrain model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            model.setCreateTime(new Date());
            cjTrainRepository.saveAndFlush(model);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean deleteTrain(String id){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            List<CjTrainDetail> list= cjTrainDetailRepository.findByTrainId(id);
            if(list != null && list.size()>0){
                resultBean.setResultCode(ResultBean.FAIL);
                resultBean.setResultDesc("已有人报名，不能删除");
                return resultBean;
            }
            cjTrainRepository.delete(id);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean modifyTrain(Map map){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjTrain c = cjTrainRepository.findOne(map.get("id").toString());
            if (StringUtil.isNotEmpty(map.get("trainTime").toString())){
                map.put("trainTime", DateUtil.getDate( map.get("trainTime").toString()));
            }
            if (StringUtil.isNotEmpty(map.get("endTime").toString())){
                map.put("endTime", DateUtil.getDate( map.get("endTime").toString()));
            }
            BeanUtils.setBeanData(c, map);
            cjTrainRepository.saveAndFlush(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean queryTrain(){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            resultBean.setRows(cjTrainRepository.findAllTrainOrderByCreateTimeDesc());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
